package com.shangcheng.server.controller;

import com.shangcheng.model.domain.OmsOrder;
import com.shangcheng.model.dto.OmsOrderDto;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.OmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/13 0013 zihao
 */
@RestController
@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private OmsOrderService omsOrderService;

    @GetMapping("/list")
    public ResponseEntity lists(@RequestParam Map<String, String> map, @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult lists = omsOrderService.lists(map, pageNum, pageSize);
        return ResponseEntity.ok(new Result(200, lists, "成功"));
    }

//批量关闭订单
    @PostMapping("/update/close")
    public ResponseEntity close(Long[] ids, String note) {
        //调用关闭
        omsOrderService.close(ids,note);
        return ResponseEntity.ok(new Result(200, "", "成功"));
    }

    //批量发货
    //前端传入的是deliveryParamList集合,内有deliveryCompany物流公司,deliverySn 物流单号,orderId 订单id
    @PostMapping("/update/delivery")
    public ResponseEntity delivery(@RequestBody List<OmsOrderDto> omsOrders) {
        //修改
        omsOrderService.delivery(omsOrders);
        return ResponseEntity.ok(new Result(200, "", "成功"));
    }

    //修改订单费用信息
    @PostMapping("/update/moneyInfo")
    public ResponseEntity moneyInfo(@RequestBody OmsOrderDto omsOrderDto) {
        //修改
        omsOrderService.moneyInfo(omsOrderDto);
        return ResponseEntity.ok(new Result(200, "", "成功"));
    }

    //备注订单
    @PostMapping("/update/note")
    public ResponseEntity note(String id,String note,String status) {
        //修改
        omsOrderService.note(id,note,status);
        return ResponseEntity.ok(new Result(200, "", "成功"));
    }

    //修改收货人信息
    @PostMapping("/update/receiverInfo")
    public ResponseEntity receiverInfo(@RequestBody OmsOrderDto omsOrderDto) {
        //修改
        omsOrderService.receiverInfo(omsOrderDto);
        return ResponseEntity.ok(new Result(200, "", "成功"));
    }

    //获取订单详情：订单信息、商品信息、操作记录
    @GetMapping("{id}")
    public ResponseEntity getById(@PathVariable String id) {
        //查询
       OmsOrderDto omsOrderDto = omsOrderService.getByIdd(id);
        return ResponseEntity.ok(new Result(200, omsOrderDto, "成功"));
    }

    //批量删除订单
    @PostMapping("/delete")
    public ResponseEntity delete(Long[] ids) {
        //删除
        boolean deletes = omsOrderService.deletes(ids);
        
        if (deletes){
            return ResponseEntity.ok(new Result(200, "", "成功"));
        }else {
            return ResponseEntity.ok(new Result(403, "", "订单未关闭"));
        }
    }

}
